<?php

/**
 * Crc CMS
 *
 * @category   Crc
 * @package    Crc_Modules
 * @subpackage User
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 * @version    $Id: AclRoles.php 39 2012-08-14 16:29:34Z razvanvlad1982@gmail.com $
 */

/**
 * Form for user roles
 *
 * @category   Crc
 * @package    Crc_Modules
 * @subpackage User
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 */

class Modules_User_Forms_AclRole extends Crc_Form
{
    /**
     * Sub forms
     *
     * @var array
     */
    public $subForms;

     /**
     * Id parent. The parent id of the role. For edit
     *
     * @var array
     */
    protected $_idParent;

    /**
     * Constructor
     *
     * Registers options for the form
     *
     * @param array $options
     * @return void
     */
    public function __construct($options = array())
    {
        $this->subForms = array(
            'save'     => array('id_parent', 'name'),
            'resource' => array('resource')
        );
        parent::__construct($options);
    }

    /**
     * Load form elements
     *
     * Registers options for the form
     *
     * @param string $subForm
     * @param string $formName
     * @return Zend_Form_SubForm
     */
    public function loadElements($subForm, $formName)
    {
        if (isset($this->subForms[$formName]) && is_array($this->subForms[$formName]) && !empty($this->subForms[$formName]))
        {
            foreach ($this->subForms[$formName] as $k => $v)
            {
                switch ($v)
                {
                    case 'name':
                        $element = new Zend_Form_Element_Text($v);
                        $element->setLabel('Name');
                        $element->setRequired(true)
                                ->addValidator(new Zend_Validate_NotEmpty(), true)
                                ->addValidator(new Zend_Validate_Regex(array('pattern' => '~^[A-Za-z0-9-_ ]*$~')), true);
                        $subForm->addElement($element);
                        break;

                    case 'id_parent':
                        $element = new Zend_Form_Element_Select($v);
                        $element->setLabel('Parent');
                        $roles = new Application_Model_Collection_AclRole();
                        $idParent = ($this->getCustomValue('idParent') > 0) ? $this->getCustomValue('idParent') : 0;
                        $arrRoles = $roles->getRolesReccuring($idParent);
                        $element->addMultiOption('', 'Choose');
                        foreach ($arrRoles as $roleID => $role) {
                            $element->addMultiOption($roleID, $role['name']);
                        }
                        $element->setRequired(true);
                        $subForm->addElement($element);
                        break;

                    case 'resource':
                        $modeResource = new Application_Model_Collection_AclResource();
                        $resources = $modeResource->fetchAll(null, 'resource ASC');
                        foreach ($resources as $resource)
                        {
                            $element = new Zend_Form_Element_Radio('[' . $resource->getId() . ']');
                            $element->setLabel($resource->getResource());
                            $element->addMultiOptions(array(0 => "No", 1 => "Yes"));
                            $subForm->addElement($element);
                        }
                        break;
                }
            }
        }
        $subForm->setOptions(array('legend' => $formName));
        return $subForm;
    }
}